package cn.wolfcode.luowowo;


import cn.wolfcode.luowowo.website.intercepter.LoginIntercepter;
import cn.wolfcode.luowowo.website.web.resolver.UserInfoArgumentResolver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@SpringBootApplication
public class WebsiteServer implements WebMvcConfigurer {
    /**
     * 创建登录拦截器
     * @return
     */
    @Bean
    public LoginIntercepter loginIntercepter(){
        return new LoginIntercepter();
    }

    /**
     * 创建参数解析器
     * @return
     */
    @Bean
    public UserInfoArgumentResolver userInfoArgumentResolver(){
        return new UserInfoArgumentResolver();
    }

    /**
     * 将参数解析器放到水平容器中,
     * @param resolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(userInfoArgumentResolver());
    }

    /**
     * 将我们的拦截器添加到web容器中
     * @param registry  配置拦截器   springboot配置拦截器的方式
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginIntercepter())
                .addPathPatterns("/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/js/**")
                .excludePathPatterns("/styles/**")
                .excludePathPatterns("/login.html")
                .excludePathPatterns("/regist.html");
    }


    public static void main(String[] args) {
        SpringApplication.run(WebsiteServer.class,args);
    }
}
